<?php
namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;

class RechargeRepeated extends Command
{
    private $start_date = '';
    private $end_date = '';
    protected function configure()
    {
        $this->setName('recharge_repeated_stats')
            ->addArgument('action', Argument::OPTIONAL, "方法名称")
            ->addOption('start_date', null, Option::VALUE_REQUIRED, '开始日期')
            ->addOption('end_date', null, Option::VALUE_REQUIRED, '结束日期')
            ->setDescription('订单复充数据汇总');
    }

    protected function execute(Input $input, Output $output)
    {

        $action = $input->getArgument('action');

        if (method_exists($this, $action)) {

            $start_date = $input->hasOption('start_date') ? $input->getOption('start_date') : date('Y-m-d', strtotime('-1days'));
            $end_date = $input->hasOption('end_date') ? $input->getOption('end_date') : date('Y-m-d', strtotime('-1days'));

            $this->start_date = $input->hasOption('start_date') ? $input->getOption('start_date') : '';
            $this->end_date = $input->hasOption('end_date') ? $input->getOption('end_date') : '';

            $this->$action($start_date, $end_date);
        }

    }

    /*刷新当日数据*/
    public function daily_channel_today($start_date,$end_date)
    {
        $start_date = date('Y-m-d');
        $end_date = date('Y-m-d');
        $this->daily_channel($start_date,$end_date);
    }

    /*刷新当日数据*/
    public function daily_agent_today($start_date,$end_date)
    {
        $start_date = date('Y-m-d');
        $end_date = date('Y-m-d');
        $this->daily_agent($start_date,$end_date);
    }

    /*刷新当日数据*/
    public function daily_today($start_date,$end_date)
    {
        $start_date = date('Y-m-d');
        $end_date = date('Y-m-d');
        $this->daily($start_date,$end_date);
    }

    /*复充报表 -- 投放号维度*/
    public function daily_channel($start_date,$end_date)
    {
        if ($start_date == $end_date && $end_date != date('Y-m-d')) {
            $start_date = date('Y-m-d', strtotime('-1days'));
            $end_date = date('Y-m-d', strtotime('-1days'));
        }

        $this->process($start_date,$end_date,'channel');
    }

    /*复充报表 -- 代理维度*/
    public function daily_agent($start_date,$end_date)
    {
        if ($start_date == $end_date && $end_date != date('Y-m-d') ) {
            $start_date = date('Y-m-d', strtotime('-1days'));
            $end_date = date('Y-m-d', strtotime('-1days'));
        }

        $this->process($start_date,$end_date,'agent');
    }

    /*复充报表 -- 运营或超管维度*/
    public function daily($start_date,$end_date)
    {
        if ($start_date == $end_date && $end_date != date('Y-m-d') ) {
            $start_date = date('Y-m-d', strtotime('-1days'));
            $end_date = date('Y-m-d', strtotime('-1days'));
        }

        $date_rang = getDateByRange($start_date, $end_date);

        foreach ($date_rang as $date) {

            $new_user_num = Db::table('quick_user')
                ->where('regdate',$date)
                ->count('id');

            $jiazhuo_num = Db::table("quick_user_add_shortcut")->where("create_date", $date)->count();

            $cost = Db::table('rp_cost')
                ->where('cost_date',$date)
                ->sum('money');

            if($cost < 1)
            {
                $cost = Db::table('rp_cost_manual')
                    ->where('cost_date',$date)
                    ->where('type',1)
                    ->sum('money');
            }

            $rechargers_num = Db::table('pay_order_success')
                ->where('status',2)
                ->where('user_reg_date',$date)
                ->where('recharge_times','>',1)
                ->where('create_date', $date)->group('uid')->count('id');

            $jiazhuo_cost = getQuotient($cost,$jiazhuo_num);

            $record = Db::table('rp_recharge_repeated_daily')
                ->where(['report_date'=>$date])->field('id')->find();

            if($record){

                Db::table('rp_recharge_repeated_daily')->where(['id'=>$record['id']])->update([
                    'new_user_num'                  => $new_user_num,
                    'new_jiazhuo_num'               => $jiazhuo_num,
                    'cost'                          => $cost,
                    'rechargers_num'                => $rechargers_num,
                    'jiazhuo_cost'                  => $jiazhuo_cost,
                ]);

            }else{

                Db::table('rp_recharge_repeated_daily')->insert([
                    'new_user_num'                  => $new_user_num,
                    'new_jiazhuo_num'               => $jiazhuo_num,
                    'cost'                          => $cost,
                    'rechargers_num'                => $rechargers_num,
                    'jiazhuo_cost'                  => $jiazhuo_cost,
                    'report_date'                   => $date,
                ]);
            }

        }

        if ($this->start_date && $this->end_date) {
            $end_date_t = $this->end_date;
            $start_date_t = $this->start_date;
        } else {
            $end_date_t = date('Y-m-d');
            $start_date_t = date('Y-m-d', strtotime('-5days'));
        }

        $list_t = Db::table('rp_recharge_repeated_daily')
            ->where('report_date','>=',$start_date_t)
            ->where('report_date','<=',$end_date_t)
            ->field('id,report_date,cost,rechargers_num')
            ->select();

        foreach ($list_t as $lv)
        {
            $this->updateRateRecovery($lv,$lv['report_date'],'admin');
        }

    }

    /*共有任务处理*/
    private function process($start_date,$end_date,$role)
    {
        $date_rang = getDateByRange($start_date, $end_date);

        $roleId = Db::table('cms_manager_role')->where('alias',$role)->value('id');
        $accounts = Db::table('cms_manager_user')->where('role_id',$roleId)->column('id');
        $accounts[] = 0;

        $afield = $role == 'channel' ? 'channel_id' : 'agent_id';

        foreach ($date_rang as $date) {

            foreach ($accounts as $account_id)
            {
                $new_user_num = Db::table('quick_user')
                    ->where('source_'.$afield,$account_id)
                    ->where('regdate',$date)
                    ->count('id');

                $jiazhuo_num = Db::table("quick_user_add_shortcut")->where($afield, $account_id)->where("create_date", $date)->count();

                if($role == 'channel')
                {
                    $account_id_gh = [$account_id];
                }else{
                    $account_id_gh = Db::table('cms_manager_user')->where('parent_id',$account_id)->column('id');
                }

                $cost = Db::table('rp_cost')->where('channel_id','in',$account_id_gh)
                    ->where('cost_date',$date)
                    ->sum('money');

                if($cost < 1)
                {
                    $cost = Db::table('rp_cost_manual')
                        ->where('channel_id','in',$account_id_gh)
                        ->where('type',1)
                        ->where('cost_date',$date)
                        ->sum('money');
                }

                $rechargers_num = Db::table('pay_order_success')
                    ->where($afield, $account_id)
                    ->where('status',2)
                    ->where('user_reg_date',$date)
                    ->where('recharge_times','>',1)
                    ->where('create_date', $date)->group('uid')->count('id');

                $jiazhuo_cost = getQuotient($cost,$jiazhuo_num);

                $record = Db::table('rp_recharge_repeated_daily_'.$role)
                    ->where([$afield=>$account_id,'report_date'=>$date])->field('id')->find();

                if($record){

                    Db::table('rp_recharge_repeated_daily_'.$role)->where(['id'=>$record['id']])->update([
                        'new_user_num'                  => $new_user_num,
                        'new_jiazhuo_num'               => $jiazhuo_num,
                        'cost'                          => $cost,
                        'rechargers_num'                => $rechargers_num,
                        'jiazhuo_cost'                  => $jiazhuo_cost,
                    ]);

                }else{

                    Db::table('rp_recharge_repeated_daily_'.$role)->insert([
                        'new_user_num'                  => $new_user_num,
                        'new_jiazhuo_num'               => $jiazhuo_num,
                        'cost'                          => $cost,
                        'rechargers_num'                => $rechargers_num,
                        'jiazhuo_cost'                  => $jiazhuo_cost,
                        'report_date'                   => $date,
                        $afield                         => $account_id
                    ]);
                }

            }
        }


        if ($this->start_date && $this->end_date) {
            $end_date_t = $this->end_date;
            $start_date_t = $this->start_date;
        } else {
            $end_date_t = date('Y-m-d');
            $start_date_t = date('Y-m-d', strtotime('-5days'));
        }

        if($role == 'channel')
        {
            $list_t = Db::table('rp_recharge_repeated_daily_channel')
                ->where('report_date','>=',$start_date_t)
                ->where('report_date','<=',$end_date_t)
                ->field('id,channel_id,report_date,cost,rechargers_num')
                ->select();
        }else if($role == 'agent'){
            $list_t = Db::table('rp_recharge_repeated_daily_agent')
                ->where('report_date','>=',$start_date_t)
                ->where('report_date','<=',$end_date_t)
                ->field('id,agent_id,report_date,cost,rechargers_num')
                ->select();
        }

        foreach ($list_t as $lv)
        {
            $this->updateRateRecovery($lv,$lv['report_date'],$role);
        }
    }

    /*更新指定日期回收率*/
    private function updateRateRecovery($lv,$report_date,$role)
    {

        $report_date = date('Y-m-d', strtotime($report_date) );
        $end_date = date('Y-m-d', strtotime('+3 day', strtotime($report_date)));

        $date_rang = getDateByRange($report_date, $end_date);
        $data = $this->getT6RangeRateRecovery($lv,$date_rang,$end_date,$role);

        if($role == 'channel')
        {
            Db::table('rp_recharge_repeated_daily_channel')->where('id',$lv['id'])->update($data);
        }else if($role == 'agent')
        {
            Db::table('rp_recharge_repeated_daily_agent')->where('id',$lv['id'])->update($data);
        }else{
            Db::table('rp_recharge_repeated_daily')->where('id',$lv['id'])->update($data);
        }

    }

    /*获取t2-t6回收率*/
    private function getT6RangeRateRecovery($lv,$date_rang,$end_date,$role)
    {
        $data = [];
        $i = 1;

        foreach ($date_rang as $date)
        {
            //复充人数
            if($role == 'channel')
            {
                $data['t'.$i] = Db::table('pay_order_success')
                    ->where('channel_id', $lv['channel_id'])
                    ->where('status',2)
                    ->where('recharge_times','>',1)
                    ->where('user_reg_date',$lv['report_date'])
                    ->where('create_date', $date)->group('uid')->count('id');
            }else if($role == 'agent')
            {
                $data['t'.$i] = Db::table('pay_order_success')
                    ->where('agent_id', $lv['agent_id'])
                    ->where('status',2)
                    ->where('recharge_times','>',1)
                    ->where('user_reg_date',$lv['report_date'])
                    ->where('create_date', $date)->group('uid')->count('id');
            }else{
                $data['t'.$i] = Db::table('pay_order_success')
                    ->where('status',2)
                    ->where('recharge_times','>',1)
                    ->where('user_reg_date',$lv['report_date'])
                    ->where('create_date', $date)->group('uid')->count('id');
            }

            $i++;

        }

        if($role == 'channel')
        {
            $total_num = Db::table('pay_order_success')
                ->where('channel_id', $lv['channel_id'])
                ->where('status',2)
                ->where('user_reg_date',$lv['report_date'])
                ->where('create_date','>=', $lv['report_date'])
                ->where('create_date','<=', $end_date)->group('uid')->count('id');

            $fc_total_num = Db::table('pay_order_success')
                ->where('channel_id', $lv['channel_id'])
                ->where('status',2)
                ->where('recharge_times','>',1)
                ->where('user_reg_date',$lv['report_date'])
                ->where('create_date','>=', $lv['report_date'])
                ->where('create_date','<=', $end_date)->group('uid')->count('id');

            $total_money = Db::table('pay_order_success')
                ->where('channel_id',$lv['channel_id'])
                ->where('create_date','<=', $end_date)
                ->where('create_date','>=', $lv['report_date'])
                ->where('user_reg_date',$lv['report_date'])
                ->where('status',2)->sum('total_fee');

        }else if($role == 'agent')
        {
            $total_num = Db::table('pay_order_success')
                ->where('agent_id', $lv['agent_id'])
                ->where('status',2)
                ->where('user_reg_date',$lv['report_date'])
                ->where('create_date','>=', $lv['report_date'])
                ->where('create_date','<=', $end_date)->group('uid')->count('id');

            $fc_total_num = Db::table('pay_order_success')
                ->where('agent_id', $lv['agent_id'])
                ->where('status',2)
                ->where('recharge_times','>',1)
                ->where('user_reg_date',$lv['report_date'])
                ->where('create_date','>=', $lv['report_date'])
                ->where('create_date','<=', $end_date)->group('uid')->count('id');

            $total_money = Db::table('pay_order_success')
                ->where('agent_id',$lv['agent_id'])
                ->where('create_date','<=', $end_date)
                ->where('create_date','>=', $lv['report_date'])
                ->where('user_reg_date',$lv['report_date'])
                ->where('status',2)->sum('total_fee');
        }else{
            $total_num = Db::table('pay_order_success')
                ->where('status',2)
                ->where('user_reg_date',$lv['report_date'])
                ->where('create_date','>=', $lv['report_date'])
                ->where('create_date','<=', $end_date)->group('uid')->count('id');

            $fc_total_num = Db::table('pay_order_success')
                ->where('status',2)
                ->where('recharge_times','>',1)
                ->where('user_reg_date',$lv['report_date'])
                ->where('create_date','>=', $lv['report_date'])
                ->where('create_date','<=', $end_date)->group('uid')->count('id');


            $total_money = Db::table('pay_order_success')
                ->where('create_date','<=', $end_date)
                ->where('create_date','>=', $lv['report_date'])
                ->where('user_reg_date',$lv['report_date'])
                ->where('status',2)->sum('total_fee');

        }

        $data['accumulated_recharge_money'] = $total_money;
        $data['accumulated_recharge_num'] = $total_num;
        $data['accumulated_rechargers_num'] = $fc_total_num;
        $data['profit'] =  $data['accumulated_recharge_money'] > 0 ? $data['accumulated_recharge_money']-$lv['cost'] : 0;
        $data['order_cost'] = getQuotient($lv['cost'],$data['accumulated_recharge_num']);

        unset($data['t1']);
        return $data;
    }
}